#include "argparser.hpp"
#include "calculator.hpp"
#include <iomanip>
#include <iostream>
#include <memory>
int main(int argc, char **argv) {
  ArgParser parser(argc, argv);
  auto args = parser.parse();

  if (args.isFailure()) {
    std::cerr << args.message << std::endl;
    return 1;
  }

  if (args.isHelp()) {
    std::cout << args.message << std::endl;
    return 0;
  };
  Func *f = Func::create_by_type(args.params.function);
  std::cout << "###########################" << std::endl;
  std::cout << "Parsed arguments:" << std::endl;
  std::cout << "Function to integrate: " << f->get_function_representation()
            << std::endl;
  delete f;
  std::cout << "Method: " << to_string(args.params.method) << std::endl;
  std::cout << "---------------------------" << std::endl;
  IntegralCalculator calculator(args.params.function, args.params.method,
                                args.params.a, args.params.b, args.params.eps);
  auto calculation_result = calculator.calculate(4);
  std::cout << "Result:\n"
            << "Calculated result: " << calculation_result.result << "\n"
            << "Number of intervals: " << calculation_result.n << "\n"
            << "Error: " << calculation_result.error << " (" << std::fixed
            << std::setprecision(2) << calculation_result.error * 100 << "%)"
            << std::endl;
  return 0;
}